package org.opens.javaskill.easypoi.entity;

import cn.afterturn.easypoi.excel.annotation.*;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 说明:
 * 1. @ExcelTarget注解说明:
 *  - value: 唯一标识;
 *  - @ExcelTarget 表示使用teacherEntity这个对象是可以针对不同字段做不同处理
 *      1. 同样的ExcelEntity 和ExcelCollection 都支持这种方式
 *
 */
@ExcelTarget(value = "easyPoiEntity")
@Data
public class EasyPoiEntity {

    public static final Random random = new Random(System.currentTimeMillis());

    /**
     * 说明:
     * 1. @Excel属性说明:
     *  - isColumnHidden属性用来控制这一列是否显示;
     *  - name控制导出列的名称;
     *  - width控制导出列的宽度;
     *  - type: 导出类型 1 是文本 2 是图片,3 是函数,10 是数字 默认是文本
     */
    @Excel(name = "id", width = 20, isColumnHidden = false)
    private Integer id;

    /**
     * 说明:
     * 1. replace属性使用下划线作为分隔符, 控制值的序列化.
     */
    @Excel(name = "性别", width = 20, replace = {"男_true", "女_false"}, groupName = "基本信息")
    private Boolean sexFlag;

    @Excel(name = "姓名", width = 20, groupName = "基本信息")
    private String name;

    @Excel(name = "年龄", width = 20, groupName = "基本信息")
    private Integer age;

    @Excel(name = "薪水", width = 20)
    private Float salary;

    @Excel(name = "余额", width = 20)
    private Double moneyTotal;

    @Excel(name = "生日", width = 20, format = "yyyy-MM-dd HH:mm:ss")
    private Date birthday;

    @Excel(name = "装填", width = 20, replace = {"初始化_0", "正常_1", "注销_2"})
    private Integer status;

    @ExcelEntity
    private Department department;

    /**
     * 说明:
     * 1. orderNum控制导出列的顺序, 默认为"0", 越小越在excel的左边;
     */
    @ExcelCollection(name = "孩子们信息", orderNum = "4")
    private List<ChildEntity> childEntities;

    public static List<EasyPoiEntity> getRandomList(int number) {
        List<EasyPoiEntity> result = new ArrayList<>();
        for (int i = 0; i < number; i++) {
            result.add(getRandomOne());
        }
        return result;
    }

    public static EasyPoiEntity getRandomOne() {
        EasyPoiEntity entity = new EasyPoiEntity();
        entity.setId(random.nextInt(1000));
        entity.setSexFlag(random.nextBoolean());
        entity.setName("from random" + random.nextInt(1000));
        entity.setSalary(random.nextFloat());
        entity.setMoneyTotal(random.nextDouble());
        entity.setBirthday(new Date(System.currentTimeMillis() - random.nextInt(1000)));
        entity.setDepartment(Department.getRandomOne());
        entity.setAge(random.nextInt(100));
        entity.setStatus(1);
        entity.setChildEntities(ChildEntity.getRandomList(random.nextInt(3)));
        return entity;
    }

}
